//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#ifndef _BOARD_PROTOCOL_H_
#define _BOARD_PROTOCOL_H_

#include "depend.h"

#ifndef _MAX_PATH
#define _MAX_PATH 260
#endif

struct request_header
{
    int reqno;
    size_t reqsize;
    UInt32 buffer_off;
};

struct reply_header
{
    UInt32 errcode;
    size_t reply_size;
};

struct request_max_size
{
    int pad[4096/sizeof(int)];
};

struct GetFileAttributes_request
{
    struct request_header __header;
};

struct GetFileAttributes_reply
{
    struct reply_header __header;
    UInt32 attr;
};

struct SetFileAttributes_request
{
    struct request_header __header;
    UInt32 attr;
};

struct SetFileAttributes_reply
{
    struct reply_header __header;
};

struct DeleteFile_request
{
    struct request_header __header;
};

struct DeleteFile_reply
{
    struct reply_header __header;
};

struct MoveFile_request
{
    struct request_header __header;
    UInt32 NewFileNameBuf;
};

struct MoveFile_reply
{
    struct reply_header __header;
};

struct CreateDirectory_request
{
    struct request_header __header;
};

struct CreateDirectory_reply
{
    struct reply_header __header;
};

struct RemoveDirectory_request
{
    struct request_header __header;
};

struct RemoveDirectory_reply
{
    struct reply_header __header;
};

struct CreateFile_request
{
    struct request_header __header;
    UInt32 desiredAccess;
    UInt32 shareMode;
    UInt32 creationDisposition;
    UInt32 flagsAndAttributes;
    PVoid hTemplateFile;
};

struct CreateFile_reply
{
    struct reply_header __header;
    PVoid hFile;
};

struct GetFileTime_request
{
    struct request_header __header;
    PVoid hFile;
};

struct GetFileTime_reply
{
    struct reply_header __header;
    FILETIME CreationTime;
    FILETIME LastAccessTime;
    FILETIME LastWriteTime;
};

struct SetFilePointer_request
{
    struct request_header __header;
    PVoid hFile;
    Int32 DistanceToMove;
    //Int32 DistanceToMoveHigh;
    UInt32 MoveMethod;
};

struct SetFilePointer_reply
{
    struct reply_header __header;
    Int32 DistanceToMove;
    //Int32 DistanceToMoveHigh;
};

struct ReadFile_request
{
    struct request_header __header;
    PVoid hFile;
    UInt32 NumberOfBytesToRead;
};

struct ReadFile_reply
{
    struct reply_header __header;
    UInt32 NumberOfBytesRead;
};

struct WriteFile_request
{
    struct request_header __header;
    PVoid hFile;
    UInt32 NumberOFBytesToWrite;
};

struct WriteFile_reply
{
    struct reply_header __header;
    UInt32 NumberOfBytesWritten;
};

struct GetFileSize_request
{
    struct request_header __header;
    PVoid hFile;
};

struct GetFileSize_reply
{
    struct reply_header __header;
    UInt32 FileSizeLow;
    //UInt32 FileSizeHigh;
};

struct SetEndOfFile_request
{
    struct request_header __header;
    PVoid hFile;
};

struct SetEndOfFile_reply
{
    struct reply_header __header;
};

struct GetDiskFreeSpace_request
{
    struct request_header __header;
};

struct GetDiskFreeSpace_reply
{
    struct reply_header __header;
    ULARGE_INTEGER FreeBytesAvailable;
    ULARGE_INTEGER TotalNumberOfBytes;
    ULARGE_INTEGER TotalNumberOfFreeBytes;
};

struct FindFirstFile_request
{
    struct request_header __header;
};

struct FindFirstFile_reply
{
    struct reply_header __header;
    IO_FIND_DATA FindFileData;
    PVoid hFindFile;
};

struct FindNextFile_request
{
    struct request_header __header;
    PVoid hFindFile;
};

struct FindNextFile_reply
{
    struct reply_header __header;
    IO_FIND_DATA FindFileData;
};

struct FindClose_request
{
    struct request_header __header;
    PVoid hFindFile;
};

struct FindClose_reply
{
    struct reply_header __header;
};

struct CloseHandle_request
{
    struct request_header __header;
    PVoid hFile;
};

struct CloseHandle_reply
{
    struct reply_header __header;
};

#define REQUEST_TABLE \
RTENT(GetFileAttributes) \
RTENT(SetFileAttributes) \
RTENT(DeleteFile) \
RTENT(MoveFile) \
RTENT(CreateDirectory) \
RTENT(RemoveDirectory) \
RTENT(CreateFile) \
RTENT(GetFileTime) \
RTENT(SetFilePointer) \
RTENT(ReadFile) \
RTENT(WriteFile) \
RTENT(GetFileSize) \
RTENT(SetEndOfFile) \
RTENT(GetDiskFreeSpace) \
RTENT(FindFirstFile) \
RTENT(FindNextFile) \
RTENT(FindClose) \
RTENT(CloseHandle) \

enum request
{
#define RTENT(x) REQ_##x,
    REQUEST_TABLE
#undef RTENT

    REQ_NB_REQUESTS
};

union generic_request
{
    struct request_max_size max_size;
    struct request_header request_header;
#define RTENT(x) struct x##_request x##_request;
    REQUEST_TABLE
#undef RTENT
};

union generic_reply
{
    struct request_max_size max_size;
    struct reply_header reply_header;
#define RTENT(x) struct x##_reply x##_reply;
    REQUEST_TABLE
#undef RTENT
};

union __server_request_info
{
    union generic_request req;    /* request structure */
    union generic_reply   reply;  /* reply structure */
};

#define SERVER_START_REQ(type) \
    do { \
        union __server_request_info *__req = ( union __server_request_info *)__VA(IOAddress); \
        struct type##_request * const req = &__req->req.type##_request; \
        const struct type##_reply * const reply = &__req->reply.type##_reply; \
        memset( &__req->req, 0, sizeof(__req->req) ); \
        __req->req.request_header.reqno = REQ_##type; \
        __req->req.request_header.reqsize = 8 * 1024; \
        __req->req.request_header.buffer_off = 4096; \
        (void)reply; \
        do

#define SERVER_END_REQ \
        while(0); \
    } while(0)

#endif
